package com.white.ipcsocket;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

public class ServerService extends Service
{

    private boolean isDestroyed;

    private static final String[] msgs = {
            "你好啊！",
            "叫你什么名字啊？",
            "一起吃个饭吧！",
            "嗨！"
    };

    public ServerService()
    {
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        createServer();
    }

    private void createServer()
    {
        new Thread(new ServerListener()).start();
    }

    private ServerSocket serverSocket;

    private class ServerListener implements Runnable
    {

        @Override
        public void run()
        {
            try
            {
                serverSocket = new ServerSocket(8798);
            } catch (IOException e)
            {
                Log.e("s", "create server failed in port:8798");
                e.printStackTrace();
                return;
            }
            while(!isDestroyed)
            {
                try
                {
                    final Socket clientSocket = serverSocket.accept();
                    Log.e("s", "accept a client");
                    new Thread()
                    {
                        @Override
                        public void run()
                        {
                            responseClient(clientSocket);
                        }
                    }.start();
                } catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

    private void responseClient(Socket clientSocket)
    {
        BufferedWriter wr = null;
        BufferedReader br = null;
        try
        {
            wr = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream(), "utf-8"));
            Random rd = new Random();
            wr.write(msgs[rd.nextInt(msgs.length)]);
            wr.flush();
            br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), "utf-8"));
            while(!isDestroyed)
            {
                String res = br.readLine();
                if(TextUtils.isEmpty(res))
                {
                    Log.e("s", clientSocket.getInetAddress() + " is log off");
                }
                Log.e("s", "client say : " + res);
            }
        } catch (IOException e)
        {
            e.printStackTrace();
        }finally
        {
            try
            {
                if(wr != null)
                {
                    wr.close();
                }
                if(br != null)
                {
                    br.close();
                }
                clientSocket.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onDestroy()
    {
        isDestroyed = true;
        try
        {
            serverSocket.close();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }
}
